package se.thoka.twt.client.utils.validation;

import se.thoka.twt.client.utils.I18N;
import se.thoka.twt.client.utils.StringUtils;

/**
 * <p>
 * Asserts that the value is either <code>null</code> (empty) or can be parsed
 * into a time.
 * </p>
 * 
 * @author Thobias Karlsson, Thoka
 * @since 9 jan 2010
 */
public class TimeOrNullValidation implements Validation {
	/**
	 * The message.
	 */
	private String message;

	/**
	 * Creates a new instance of {@link TimeOrNullValidation}.
	 */
	public TimeOrNullValidation() {
		this(ValidationUtil.getStrings().timeIncorrectFormatted(
				I18N.getTimeFormat().getPattern()));
	}

	/**
	 * Creates a new instance of {@link TimeOrNullValidation}.
	 * 
	 * @param message
	 *            The message.
	 */
	public TimeOrNullValidation(String message) {
		this.message = message;
	}

	@Override
	public String getDescription() {
		return message;
	}

	@Override
	public boolean validate(String value) {
		return StringUtils.nullOrEmpty(value)
				|| I18N.tryParseTime(value) != null;
	}

}
